/*
 * @Author: your name
 * @Date: 2020-08-22 09:31:55
 * @LastEditTime: 2020-08-26 21:39:13
 * @LastEditors: 刘思源
 * @Description: In User Settings Ed
 * @FilePath: \examlist\src\store\getAuth\getAuth.js
 */

import { getStudent, deleteStudent, putStudent } from '../../api/student'
import { Message } from 'view-design'
const state = {
  studentList: [], // 全部已经分配学生数据
  pageList: [] // 分页数据
}

const mutations = {
  getStudents(state, payload) { // 获取全部学生数据
    state.studentList = payload
  },
  getPage(state, payload) { // 分页
    const start = (payload.pagenum - 1) * payload.pagesize
    const end = start + payload.pagesize
    state.pageList = state.studentList.slice(start, end)
    console.log(state.pageList, '---分页的数据')
  }
}
const actions = {
  async getStudentList({ commit }) { // 获取学生数据
    const { data: res } = await getStudent()
    commit('getStudents', res.data)
    // 分页的数据
    commit('getPage', { pagenum: 1, pagesize: 10 })
  },
  async deleteStudentList({ commit }, id) { // 删除学生数据
    const { data: res } = await deleteStudent(id)
    if (res.code === 1) {
      Message.success(res.msg)
    } else {
      Message.error(res.msg)
    }
  },
  async putStudentList({ commit }, form) { // 编辑学生信息数据
    const { data: res } = await putStudent(form)
    console.log(res)
    if (res.code === 1) {
      Message.success(res.msg)
    } else {
      Message.error(res.msg)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
